package com.junzhiit.nengji.shop.dao.impl;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Repository;

import com.bainaeco.framework.base.dao.AbstractBaseDAO;
import com.bainaeco.framework.common.jdbc.StatementParameter;
import com.bainaeco.framework.enums.IsDelete;
import com.junzhiit.nengji.shop.bo.GoodsInfoBO;
import com.junzhiit.nengji.shop.dao.SystemTypeDAO;
import com.junzhiit.nengji.shop.model.SystemType;

@Repository
public class SystemTypeDAOImpl extends AbstractBaseDAO<SystemType> implements SystemTypeDAO {

	@Override
	public List<SystemType> findAll() {
		String sql = "select * from `system_type` where is_delete=? order by full_code asc ";
		StatementParameter param = new StatementParameter();
		param.setInt(IsDelete.NO.toIntValue());
		return super.queryForList(sql, SystemType.class, param);
	}

	@Override
	public List<SystemType> getAllByCondition(SystemType type) {
		StringBuffer sb = new StringBuffer();
		sb.append("select * from `system_type` where 1=1 ");
		StatementParameter param = new StatementParameter();
		if(StringUtils.isNotBlank(type.getId())){
			sb.append(" and id = ? or parentId = ? ");
			param.setString(type.getId());
			param.setString(type.getId());
		}else{
			if(StringUtils.isNotBlank(type.getName())){
				sb.append(" and name like ? ");
				param.setString("%"+type.getName()+"%");
			}
			if(StringUtils.isNotBlank(type.getParentId())){
				sb.append(" and parent_id = ? ");
				param.setString(type.getParentId());
			}else{
				if(type.getLevel() !=null && !"".equals(type.getLevel())){
					sb.append(" and level = ? ");
					param.setInt(type.getLevel());
				}
			}
		}
		sb.append(" and is_delete= ? ");
		param.setInt(IsDelete.NO.toIntValue());
		sb.append(" order by sort desc ");
		return super.queryForList(sb.toString(), SystemType.class, param);
	}

	@Override
	public String getMaxCode(String parentId) {
		StatementParameter param = new StatementParameter();
		StringBuffer sql = new StringBuffer();
		sql.append("select max(code) from `system_type` where 1 = 1");
		if (StringUtils.isNotBlank(parentId)) {
			sql.append(" and parent_id = ?");
			param.setString(parentId);
		}
		return super.queryForString(sql.toString(), param);
	}

	@Override
	public String getMaxFullCode(String parentId) {
		StatementParameter param = new StatementParameter();
		StringBuffer sql = new StringBuffer();
		sql.append("select max(full_code) from `system_type` where 1 = 1");
		if (StringUtils.isNotBlank(parentId)) {
			sql.append(" and parent_id = ?");
			param.setString(parentId);
		}
		return super.queryForString(sql.toString(), param);
	}

	@Override
	public int getMaxSort(String parentId) {
		StatementParameter param = new StatementParameter();
		StringBuffer sql = new StringBuffer();
		sql.append("select ifnull(max(sort),0)  from `system_type` where 1 = 1");
		if (StringUtils.isNotBlank(parentId)) {
			sql.append(" and parent_id = ?");
			param.setString(parentId);
		}
		return super.queryForInt(sql.toString(), param);
	}

	@Override
	public int getInfoCategoryExist(SystemType type) {
		// TODO Auto-generated method stub
		StatementParameter param = new StatementParameter();

		StringBuffer sql = new StringBuffer();
		sql.append("select count(*) from `system_type` where is_delete=? ");
		param.setInt(IsDelete.NO.toIntValue());
	
		if (StringUtils.isNotBlank(type.getName())) {
			sql.append(" and name=?");
			param.setString(type.getName());
		}

		if (StringUtils.isNotBlank(type.getParentId())) {
			sql.append(" and parent_id=?");
			param.setString(type.getParentId());
		}

		return super.queryForInt(sql.toString(), param);
	}

	@Override
	public List<SystemType> getChildId(String parentId) {
		String sql = "select id from system_type where parent_id=?";
		StatementParameter param = new StatementParameter();
		param.setString(parentId);
		return super.queryForList(sql, SystemType.class, param);
	}

	@Override
	public List<SystemType> getChild(String parentId) {
		StringBuffer sql=new StringBuffer();
		sql.append(" select * from system_type where parent_id=?");
		StatementParameter param = new StatementParameter();
		param.setString(parentId);
		return super.queryForList(sql.toString(), SystemType.class, param);
	}

	@Override
	public List<GoodsInfoBO> getGoodsByType(String id,Integer isPlatform) {
		StatementParameter param = new StatementParameter();
		StringBuffer sql = new StringBuffer();
		sql.append("select g.*,s.`name` shopName,s.shop_head_img_url shopHeadImgUrl,s.type shopType,s.mobile_phone mobilePhone,s.address address from (goods g left join goods_system_type sy on g.id = sy.goods_id) left join shop s on g.shop_id = s.id ");
		sql.append(" where sy.system_type_id = ? ");
		param.setString(id);
		if(isPlatform != null){
			sql.append(" and s.is_platform = ? ");
			param.setInt(isPlatform);
		}
		
		return super.queryForList(sql.toString(), GoodsInfoBO.class, param);
	}

}
